//
//  ModelPairs.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/15.
//

#import "ModelPairs.h"

NSString *const kModelPairsKey = KEY_key;
NSString *const kModelPairsValue = KEY_value;

@interface ModelPairs ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ModelPairs

@synthesize key = _key;
@synthesize value = _value;

+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
        self.key = [self objectOrNilForKey:kModelPairsKey fromDictionary:dict];
        self.value = [self objectOrNilForKey:kModelPairsValue fromDictionary:dict];
    }
    return self;
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.key forKey:kModelPairsKey];
    [mutableDict setValue:self.value forKey:kModelPairsValue];
    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}

@end
